package com.shanghai.market.plshnaghai;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import com.shanghai.market.plshnaghai.home.LobbyFragment;
import com.shanghai.market.plshnaghai.home.MayFragment;
import com.shanghai.market.plshnaghai.home.RecordFragment;
import com.shanghai.market.plshnaghai.publics.PublicActivity;

public class TabsActivity extends PublicActivity {

    private LayoutInflater mInflater;
    private FragmentTabHost mTabHost;
    private Resources mRes;
    private Bundle bundle;
    private String[] mTabTextArray;
    private int[] mResourceArray = {R.mipmap.qq_login_btn_icon, R.mipmap.qq_login_btn_icon, R.mipmap.qq_login_btn_icon};

    // 定义数组来存放Fragment界面
    @SuppressWarnings("rawtypes")
    private Class mFragmentArray[] = {RecordFragment.class, LobbyFragment.class, MayFragment.class};

    @Override
    public void initParms() {
        mRes = getResources();
        bundle = new Bundle();
        mTabTextArray = mRes.getStringArray(R.array.tab_array);

    }

    @Override
    public View bindView() {
        View view = LayoutInflater.from(this).inflate(R.layout.activity_tabs, null);
        return view;
    }

    @Override
    public int bindLayout() {
        return 0;
    }

    @Override
    public void initView(View view) {
        // 实例化布局对象
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.layout_tabcontent);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void doBusiness(Context mContext) {
        // 得到fragment的个数
        int count = mTabTextArray.length;

        for (int i = 0; i < count; i++) {
            // 为每一个Tab按钮设置图标、文字和内容
            final TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTabTextArray[i]).setIndicator(getTabItemView(i));
            // 将Tab按钮添加进Tab选项卡中
//            bundle.putSerializable(Extra.SEARCH, SearchEnum.WARNING);
            mTabHost.addTab(tabSpec, mFragmentArray[i], bundle);
            // 设置Tab按钮的背景
        }
        mTabHost.getTabWidget().setDividerDrawable(null);

    }

    public void setCurrentTab(int tabIndex, int index) {
//        bundle.putSerializable(Extra.INDEX, index);
        mTabHost.setCurrentTab(tabIndex);

    }

    public void setCurrentTab(int tabIndex) {
        mTabHost.setCurrentTab(tabIndex);
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    @SuppressLint("InflateParams")
    private View getTabItemView(int index) {
        final View view = mInflater.inflate(R.layout.item_tab, null);
        TextView textView = (TextView) view.findViewById(R.id.text_tab);
        textView.setText(mTabTextArray[index]);
        textView.setCompoundDrawablesWithIntrinsicBounds(0, mResourceArray[index], 0, 0);

        return view;
    }
}
